<?php
/**
 * Created by PhpStorm.
 * User: yunlongw
 * Date: 2016/12/23
 * Time: 上午10:03
 */

namespace System\Model;


use Common\Model\CommonModel;

class SmsModel extends CommonModel
{
    const TYPE_1 = 1;//注册
    const TYPE_2 = 2;//忘记密码
    const TYPE_3 = 3;//支付密码修改

    /**
     * @param $type
     * @return string
     * 类型转字符串
     */
    function typeToString($type) {
        switch ($type) {
            case self::TYPE_1:
                return '注册';
            break;
            case self::TYPE_2:
                return '忘记密码';
            break;
            case self::TYPE_3:
                return '修改支付密码';
            break;
            default:
                return '';
            break;
        }
    }

    //过期时间,分钟
    private $valid_time = 15;


    /**
     *
     * @param $code
     * @param $mobile
     *
     * @return mixed
     */
    public function add_row($code, $content, $mobile = '', $type = 'register')
    {
        $data = [
            'code'        => $code,
            'content'     => $content,
            'mobile'      => $mobile,
            'type'        => $type,
            'create_time' => date('Y-m-d H:i:s', time()),
            'end_time'    => date('Y-m-d H:i:s', time() + $this->valid_time * 60),
            'ip'          => get_client_ip(),
        ];
        return $this->add($data);
    }


    /**
     * @param $mobile
     * @param $code
     * @param $type
     * @return bool
     */
    public function checkCode($mobile, $code, $type = self::TYPE_1)
    {
        $result = $this->where(['mobile' => $mobile, 'code' => $code, 'type' => $type])->order('id desc')->select();
        if ($result && strtotime($result[0]['end_time']) > date('Y-m-d H:i:s')) {
            return true;
        }
        return false;
    }
}